package cn.wolfcode.crm.web.controller;/**
 * Created by 10797 on 2018/7/12.
 */

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.service.*;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @program: crm
 * @description: 保单组件
 * @author: Mr.chen
 * @create: 2018-07-12 09:02
 **/
@Controller
@RequestMapping("carInsurance")
public class CarInsuranceController {

    @Autowired
    private IGuaranteeBillService guaranteeBillService;
    @Autowired
    private IClientService clientService;
    @Autowired
    private ICarService carService;
    @Autowired
    private IProductService productService;


    GuaranteeBill guaranteeBill = new GuaranteeBill();
    @RequestMapping("")
    public String list(){
        return "/carInsurance/list";
    }

    @RequestMapping("client")
    @ResponseBody
    @RequiresPermissions("carInsurance:client")
    @PermissionName("保单保存客户")
    public JsonResult saveClient(Long id){
        try{
            Client client = clientService.get(id);
            guaranteeBill.setPolicyClient(client);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("保存失败");
        }
        return new JsonResult();
    }

    @RequestMapping("car")
    @ResponseBody
    @RequiresPermissions("carInsurance:car")
    @PermissionName("保单保存车辆")
    public JsonResult saveCar(Car car){
        try{

            guaranteeBill.setCar(car);
            carService.saveOrUpdate(car);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("保存失败");
        }
        return new JsonResult();
    }
    @RequestMapping("product")
    @ResponseBody
    @RequiresPermissions("carInsurance:product")
    @PermissionName("保单保存产品")
    public JsonResult saveProduct(Long[] ids, Integer totalAmount, Integer burningTime){
        try{
            //保存保单和产品之间的关系,生成保单编号
            String number=productService.saveBillAndProductRelation(ids);
            //保存保单编号
            guaranteeBill.setNumber(number);
            //保存限制年限
            guaranteeBill.setLimitYear(burningTime);
            //保存总金额
            BigDecimal totalPrice = BigDecimal.valueOf(totalAmount);
            guaranteeBill.setTotalPrice(totalPrice);
            //保存业务日期
            guaranteeBill.setVdate(new Date());
            //保存业务员
            Employee opEmployee = (Employee) SecurityUtils.getSubject().getPrincipal();
            guaranteeBill.setOpEmployee(opEmployee);
            //保存保单信息
            guaranteeBillService.save(guaranteeBill);


        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("保存失败");
        }
        return new JsonResult();
    }

}
